global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"
do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do"

* All required Stata modules are available in the /_packages and /_auxiliary folders
cap adopath - "/home/site/etc/stata/ado.nber"
cap adopath - "/home/site/etc/stata/ado.nber/updates"
adopath ++ "$SSDIMed/scripts/_packages"
adopath ++ "$SSDIMed/scripts/_auxiliary"
grstyle init 

cap log close 
log using $SSDIMed/scripts/logs/04d_spending_bootstrap.log, text replace 
cap mkdir "$SSDIMed/results/estimates/modelparameterization"

***print hostname 
local hostname: env HOSTNAME
if !mi("`hostname'") di "Hostname:   `hostname'" 

****THIS FILE PREPARES MTO FIGURES WITH CIs for BASELINE and SIX VARIANTS REPORTED FOR ROBUSTNESS
***Specs for Figure fig:modelrobustness6 (A.9) that reports marginal medical spending for various entry and spending specs 
***Panel A= 54 & 55
***Panel B= Entry: County. Spending: Years enrolled X County, County X year of observation
***Panel C= Entry: Entry-Month. Spending: Years enrolled X County 
***Panel D= Entry: County. Spending: years enrolled X entry month 
***Panel E= Entry: null. Spending: Years enrolled X county 
***Panel F: Entry: county. Spending: Years enrolled. 

***Bootstrap samples prepared in 04c
***Bootstrap for spending needs 54&55, years enrolled, (Years enrolled X County, County X year of observation), and (years enrolled X entry month)





*************************************************************************************************
***STEP 1 make subsetted data

use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
keep tot_pmt age52 unemp_rate_county_atapp fipscounty_firstnm covstart_month years_since_covstart years_since_covstart rfrnc_yr county_mofd
keep if tot_pmt<.
save "$SSDIMed/data/proc/robustness_spending.dta", replace 

use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
keep if inrange(age_year_covstart_fill, 56, 57)
gen age52 = (age_year_covstart_fill == 57)
keep tot_pmt age52 unemp_rate_county_atapp fipscounty_firstnm covstart_month years_since_covstart years_since_covstart rfrnc_yr
keep if tot_pmt<.
save "$SSDIMed/data/proc/robustness_spending_5657.dta", replace 

use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
keep died_adj age52 unemp_rate_county_atapp fipscounty_firstnm covstart_month years_since_covstart years_since_covstart rfrnc_yr
save "$SSDIMed/data/proc/robustness_mortality.dta", replace 



 
****STEP 2 ITERATE OVER SPECIFICATIONS AND THEN BOOTSTRAP SAMPLES, ESTIMATE TRUE SAMPLE COEFFICIENTS AS SAMPLE==0 ENTRY 
foreach spec in 01 02 05 06 5657{
cd "$SSDIMed/results/estimates/x-age52xUR"
postfile spending_unempatappX52 intercept age52 UR age52xUR sample using "bootstrap_spending_`spec'.dta", replace 

local clust county_mofd
local y tot_pmt 
local controls_01 i.years_since_covstart
local controls_02 i.years_since_covstart##i.fipscounty_init
local controls_05 i.years_since_covstart##i.fipscounty_init i.rfrnc_yr##i.fipscounty_init
local controls_06 i.years_since_covstart##i.covstart_month
clear
if "`spec'"=="5657" use "$SSDIMed/data/proc/robustness_spending_5657.dta"
else use "$SSDIMed/data/proc/robustness_spending.dta"
sum unemp_rate_county_atapp 
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
gegen fipscounty_init=group(fipscounty_firstnm)
if "`spec'"=="5657" local ctrl "02"
else local ctrl "`spec'"
***RUN REGRESSION
***NB no clustering -- we only use the coefficients 
reghdfe `y' age52 UR age52xUR, abs(`controls_`ctrl'')
***save estimates  
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post spending_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (0)

***STEP 3 RUN BOOTSTRAP SAMPLES 
forval n=1/500{
di "`n'"
qui{
clear 
if "`spec'"=="5657" use "$SSDIMed/data/proc/robustness_spending_5657.dta"
else use "$SSDIMed/data/proc/robustness_spending.dta"
rename (fipscounty_firstnm covstart_month ) (fipscounty_init`n' covstart_month`n')
joinby fipscounty_init`n' covstart_month`n' using "$SSDIMed/data/analysis/bootstrap/samples.dta"
gegen fipscounty_init=group(fipscounty_init`n')
rename covstart_month`n' covstart_month 
sum unemp_rate_county_atapp 
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR

if "`spec'"=="5657" local ctrl "02"
else local ctrl "`spec'"
local clust county_mofd
local y tot_pmt 
local controls_01 i.years_since_covstart
local controls_02 i.years_since_covstart##i.fipscounty_init
local controls_05 i.years_since_covstart##i.fipscounty_init i.rfrnc_yr##i.fipscounty_init
local controls_06 i.years_since_covstart##i.covstart_month
reghdfe `y' age52 UR age52xUR, abs(`controls_`ctrl'')
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post spending_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (`n') 
}
}
postclose spending_unempatappX52
}


*****Step 4: MORTALITY: ITERATE OVER BOOTSTRAP SAMPLES FOR SPEC 01 ONLY
local spec "01"
cd "$SSDIMed/results/estimates/x-age52xUR"
postfile mortality_unempatappX52 intercept age52 UR age52xUR sample using "bootstrap_spending_`spec'.dta", replace 

local clust county_mofd
local y died_adj
local controls_01 i.years_since_covstart
clear
use "$SSDIMed/data/proc/robustness_mortality.dta"
sum unemp_rate_county_atapp 
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
gegen fipscounty_init=group(fipscounty_firstnm)
local ctrl "`spec'"
***RUN REGRESSION
***NB no clustering -- we only use the coefficients 
reghdfe `y' age52 UR age52xUR, abs(`controls_`ctrl'')
***save estimates  
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post spending_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (0)
forval n=1/500{
di "`n'"
qui{
clear 
use "$SSDIMed/data/proc/robustness_mortality.dta"
rename (fipscounty_firstnm covstart_month ) (fipscounty_init`n' covstart_month`n')
joinby fipscounty_init`n' covstart_month`n' using "$SSDIMed/data/analysis/bootstrap/samples.dta"
gegen fipscounty_init=group(fipscounty_init`n')
rename covstart_month`n' covstart_month 
sum unemp_rate_county_atapp 
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR

local ctrl "`spec'"
local clust county_mofd
local y died_adj 
reghdfe `y' age52 UR age52xUR, abs(`controls_`ctrl'')
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post mortality_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (`n') 
}
}
postclose mortality_unempatappX52



